// pages/order_manage/index.js
const app = getApp(), core = require('../../utils/core'), util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    packageOrderList:[],
    packageOrderListMore:{page:1},
    packageOrderIndex: 0,
    tabIdx: -1,
    payIdx: 1,
    config:{},
    showPop:false
  },
  togglePop(){
    this.setData({
      showPop: !this.data.showPop
    })
  },
  payClick(e) {
    this.setData({
      payIdx: e.currentTarget.dataset.index
    })
  },
  tabClick(e) {
    this.setData({
      tabIdx: e.currentTarget.dataset.index
    });
    this.refresh();
  },

  choosePackageOrder(e){
    let packageOrderIndex = e.currentTarget.dataset.index;
    this.setData({
      packageOrderIndex:packageOrderIndex,
      payIdx: this.data.packageOrderList[packageOrderIndex].pay_type,
      showPop: !this.data.showPop
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let page = this;
    core.get({url: '/xilujob.common/corporate',loading:false,success(ret, data) {
      page.setData({
        config: ret.data
      });
    }
  });
    this.fetchPackageOrderList();
  },

  refresh(){
    this.setData({
      packageOrderList:[],
      packageOrderListMore:{page:1},
    });
    this.fetchPackageOrderList();
  },

  //列表
  fetchPackageOrderList() {
    util.fetch(this, 'xilujob.package_order/lists', {state: this.data.tabIdx}, 'packageOrderListMore', 'packageOrderList', 'data', list=>{

    });
  },
  orderDetail(e){
    let index = e.currentTarget.dataset.index;
    let order = this.data.packageOrderList[index];
    wx.navigateTo({
      url: '/pages/order_detail/order_detail?order_id='+order.id,
    })
  },
  /**
   * 订单取消
   */
  orderCancel(event) {
    let page = this;
    let index = event.currentTarget.dataset.index;
    let order = this.data.packageOrderList[index];
    wx.showModal({
      title: '提示',
      content: '是否取消支付？',
      success: res => {
        if(res.confirm){
          core.post({
            url: '/xilujob.package_order/cancel', data: {package_order_id: order.id},
            success: ret => {
              page.refresh();
          }
          });
        }
      }
    })
    
  },

  /**
   * 套餐购买
   */
  onsubmit(){
    let packageOrderIndex = this.data.packageOrderIndex;
    let packageOrder = this.data.packageOrderList[packageOrderIndex];
    let package_order_id = packageOrder.id;
    let config = this.data.config;
    //let ticket_type = this.data.ticketType;
    //let ticket = this.data.ticket;
    let pay_type = this.data.payIdx;
    //let remarks = this.data.remarks;
    let data = {
      platform: 'wxmini',
      order_id:package_order_id,
      //ticket_type:ticket_type,
      //ticket:ticket,
      pay_type:pay_type,
      corporate_company:config.corporate_company,
      open_bank:config.open_bank,
      corporate_account:config.corporate_account,
      //remarks:remarks
    }
    let page = this;
    if(pay_type == 1){
      wx.login({
        success(wxRes) {
          let code = wxRes.code;
          core.post({
            url: '/xilujob.user/wxlogin',
            data: {code: code},
            success: (ret, response) => {
              data['third_id'] = ret.data.third.third_id;
              page.payment(data);
            },fail: (ret, response) => {
              wx.showToast({
                title: '下单失败',
                icon:'none'
              })
            }
          });
        },
      });
    }else{
      page.payment(data);
    }
  },
  payment(order){
    let page = this;
    core.post({url: '/xilujob.pay/pay',data:order,loading:true,success(ret, data) {
      if(order.pay_type == 1){
          //发起支付
          wx.requestPayment({
            'timeStamp': ret.data.timeStamp,
            'nonceStr': ret.data.nonceStr,
            'package': ret.data.package,
            'signType': ret.data.signType,
            'paySign': ret.data.paySign,
            'success': function (res) {
              wx.showToast({
                icon: "none",
                title: '支付成功',
              })
              page.togglePop();
              page.fetchCompany();
            },
            'fail': function (res) {
              wx.showToast({
                icon: "none",
                title: '支付失败',
              })
            },
            'complete': function (res) {
              
            }
          });
      }else{
        wx.showToast({
          title: ret.msg,
          icon:'none'
        })
      }
    }});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})
